package com.aiobc.mq;


import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mq生产者配置
 */
@Configuration
public class MQProducerConfigure {

    @Value("${rocketmq.name-server}")
    private String nameSrvAddress;

    @Value("${rocketmq.producer.group}")
    private String hotelRoomProducerGroup;

    /**
     * 登录生产者
     *
     * @return 登录消息rocketmq的生产者对象
     */
    @Bean
    public DefaultMQProducer defaultProducer() throws MQClientException {
        System.out.println("生产者创建中=======");
        DefaultMQProducer producer = new DefaultMQProducer(hotelRoomProducerGroup);
        producer.setNamesrvAddr(nameSrvAddress);
        //todo
        // 报错信息 ：
        //          org.apache.rocketmq.client.exception.MQClientException: The producer service state not OK, maybe start
        // start不能调用两次，如果引入RocketMQTemplate相关依赖，RocketMQTemplate自己start了
//        producer.start();
        return producer;
    }
}
